sliderV.setPreferredSize( new Dimension( 50, 300) );
ChangeListener
As the user drags the knob across a slider, change events are generated. The number of events depends on how fast the user drags the knob. Dragging slowly generates an event every time the knob crosses an integer. An event listener must potentially deal with very many events.
A listener for a slider implements the
ChangeListener
interface.
This interface has a single method:
public void stateChanged( ChangeEvent evt )
An object that implements the interface must be registered with the slider in order to receive events:
slider.addChangeListener( ChangeListener lstn )
Often, especially in small projects, the frame that contains the slider is also its listener.
Say that you have a frame class
that contains a slider and implements ChangeListener
.
Register an object of that class with its slider:
Hint: this is the same as we have been doing with frames that
implement ActionListener
and their buttons.